package com.example.spzx.controller;

import com.example.spzx.Mapper.BrandMapper;
import com.example.spzx.Mapper.ProductMapper;
import com.example.spzx.Service.ProductService;
import com.example.spzx.model.dto.product.ProductDto;
import com.example.spzx.model.entity.base.ProductUnit;
import com.example.spzx.model.entity.product.Brand;
import com.example.spzx.model.entity.product.Product;
import com.example.spzx.model.vo.common.Result;
import com.example.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/product/product")
@RestController
public class ProductController {
    @Resource
    private BrandMapper brandMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ProductService productService;
    @GetMapping("/allunit")
    public Result<ProductUnit> productUnitResult(){
        List<ProductUnit> list=productMapper.getlist();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/{page}/{limit}")
    public Result<Product>  list(@PathVariable("page") Integer page, @PathVariable("limit") Integer limit , ProductDto productDto){
        System.out.println(productDto);
        PageInfo<Product> pageInfo=productService.select(page,limit,productDto);
        return Result.build(pageInfo,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/FindBrandByCategoryId/{id}")
    public Result<Brand> FindBrandByCategoryId(@PathVariable("id") Integer id){
        System.out.println(id+"id");
        List<Brand> brand=brandMapper.getbyid(id);

        return Result.build(brand,ResultCodeEnum.SUCCESS);
    }
    @PostMapping("/save")
    public Result<Product> save(@RequestBody Product product){
        productService.save(product);
        return Result.build(null,ResultCodeEnum.SUCCESS);


    }
    @GetMapping("/getById/{id}")
    public Result<Product> getById(@PathVariable("id") Integer id){
        Product product=productService.getbyid(id);
        return Result.build(product,ResultCodeEnum.SUCCESS);
    }
    @PutMapping("/updateById")
    public Result<Product> updateById(@RequestBody Product product){
         productService.updateByid(product);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/updateAuditStatus/{id}/{astatus}")
    public Result<Product> updateAuditStatus(@PathVariable("id") Integer id,@PathVariable("astatus") Integer astatus){
        Product product=productMapper.getbyid(id);
        product.setAuditStatus(astatus);
        product.setAuditMessage(astatus == 1 ?"通过":"未通过");
    productMapper.updateByid(product);
    return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @GetMapping("/updateStatus/{id}/{status}")
    public Result<Product> updateStatus(@PathVariable("id") Integer id,@PathVariable("status") Integer status){
        Product product=productMapper.getbyid(id);
        product.setStatus(status);
        productMapper.updateByid(product);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    @DeleteMapping("/deleteById/{id}")
    public Result<Product> deleteById(@PathVariable("id") Integer id){
        productMapper.deleteskuByid(Long.valueOf(id));
        productMapper.deleteById(id);
        productMapper.deleteBydetail(id);
        productMapper.deleteByattr(id);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
}
